import jwt
import datetime
import time

# 加密
key = '1234567890'  # 加密的key，可以是任意的字符串，不能让人知道


class MyJwt:
    # 获取token
    def get_token(self, userid, account):
        user = {
            "id": userid,
            "username": account,
            "exp": datetime.datetime.utcnow() + datetime.timedelta(weeks=2)
        }
        # 生成token
        token = jwt.encode(user, key, algorithm='HS256')
        # 返回token
        return "Bearer " + token

    # 验证token
    def check_token(self, token):
        # 获取token  判断token是否存在
        # if token:
            # 分割
            # token = token.split(" ")[1]
            # try:
                # 验证token 解密token为字典
                return jwt.decode(token, key, algorithms='HS256')
            # except:
            #     raise Exception("token不合法")


jwttoken = MyJwt()
# print(jwttoken.get_token(1, 'admin'))
# print(time.time())
